from PIL import Image
from math import trunc
import os
import xml.etree.ElementTree as ET
import shutil
import cv2

def light(src,dst,light):
    # open an image file (.jpg or.png) you have in the working folder
    im1 = Image.open(src)
    # multiply each pixel by 0.9 (makes the image darker)
    # works best with .jpg and .png files, darker < 1.0 < lighter
    # (.bmp and .gif files give goofy results)
    # note that lambda is akin to a one-line function
    # im1 = cv2.imread(src)
    im2 = im1.point(lambda p: p * light)
    # im1 = img
    # size = img.shape
    # print(size)
    # h,w = size[0],size[1]
    # im2 = im1.point(lambda p: p * light)
    # brings up the modified image in a viewer, simply saves the image as
    # a bitmap to a temporary file and calls viewer associated with .bmp
    # make certain you have associated an image viewer with this file type
    # im2.show()
    # save modified image to working folder as Audi2.jpg
    im2.save(dst)

def png2jpg(src,dst):
    im = Image.open(src)
    im = im.convert('RGB')
    im.save(dst,quality=95)

VOCdevkit_path = 'png'
if __name__ == "__main__":
    pngFilePath = os.path.join(VOCdevkit_path, 'PngImages')
    pngfiles = os.listdir(pngFilePath)
    jpgFilePath = os.path.join(VOCdevkit_path, 'JPEGImages')
    if not os.path.exists(jpgFilePath):
        os.mkdir(jpgFilePath)    
    pngfiles.sort()
    for fileName in pngfiles:
        shortName = os.path.splitext(fileName)[0]
        jpgFileName = shortName + ".jpg"
        srcFile = os.path.join(pngFilePath,fileName)
        dstFile = os.path.join(jpgFilePath,shortName+'.jpg')
        png2jpg(srcFile,dstFile)
        print(dstFile)




